package org.liufeng.course.service;

import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.liufeng.course.message.req.ImageMessage;
import org.liufeng.course.message.resp.TextMessage;
import org.liufeng.course.util.MessageUtil;

/**
 * ?????????
 * 
 * @author liufeng
 * @date 2013-09-29
 */
public class CoreService {
	/**
	 * ????????????????
	 * 
	 * @param request
	 * @return xml
	 */
	public static String processRequest(HttpServletRequest request) {
		// xml????????????
		String respXml = null;
		// ?????????????????
		String respContent = "系统正在升级，请稍后再试！";
		try {
			// ????parseXml???????????????
			Map<String, String> requestMap = MessageUtil.parseXml(request);
			// ????????
			String fromUserName = requestMap.get("FromUserName");
			// ??????????
			String toUserName = requestMap.get("ToUserName");
			// ???????
			String msgType = requestMap.get("MsgType");

//			// ?????????
//			ImageMessage imageMessage = new ImageMessage();
//			imageMessage.setToUserName(fromUserName);
//			imageMessage.setFromUserName(toUserName);
//			imageMessage.setCreateTime(new Date().getTime());
//			imageMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_IMAGE);
			
			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(fromUserName);
			textMessage.setFromUserName(toUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);

			// ??????
			if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
				respContent = "txt";
			}
			// ?????
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
				respContent = "image";
			}
			// ???????
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
				respContent = "voice";
			}
			// ??????
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)) {
				respContent = "video";
			}
			// ????λ?????
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
				respContent = "location";
			}
			// ???????
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
				respContent = "link";
			}
			// ???????
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
				// ???????
				String eventType = requestMap.get("Event");
				// ???
				if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
					respContent = "关注";
				}
				// ??????
				else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
					// TODO ??????????????????????????????????????????????
				}
				// ?????????????
				else if (eventType.equals(MessageUtil.EVENT_TYPE_SCAN)) {
					// TODO ????????????????????
				}
				// ???????λ??
				else if (eventType.equals(MessageUtil.EVENT_TYPE_LOCATION)) {
					// TODO ???????????λ?????
				}
				// ???????
				else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
					// TODO ????????????
				}
			}
			// ????????????????
			textMessage.setContent(respContent);
			// ?????????????????xml
			respXml = MessageUtil.messageToXml(textMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return respXml;
	}
}
